package com.example.administrator.caigou51.activity;

import android.os.Bundle;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.example.administrator.caigou51.R;
import com.example.administrator.caigou51.bean.OrderDetailBean;
import com.example.administrator.caigou51.bean.OrderInfoBean;
import com.example.administrator.caigou51.global.App;
import com.example.administrator.caigou51.global.Constant;
import com.example.administrator.caigou51.recyclerCard.basic.MaterialListView;
import com.example.administrator.caigou51.recyclerCard.card.AddAgainGoodsCard;
import com.example.administrator.caigou51.recyclerCard.card.OrderDetail1Card;
import com.example.administrator.caigou51.recyclerCard.card.OrderDetail2Card;
import com.example.administrator.caigou51.recyclerCard.card.OrderDetail3Card;
import com.example.administrator.caigou51.request.BaseResponse;
import com.example.administrator.caigou51.request.ICallBackForRequestServer;
import com.example.administrator.caigou51.request.RequestServerManager;

import java.util.List;

/**
 * Created by Administrator on 2016/8/31/031.
 */
public class OrderDetailActivity extends GBaseActivity {
    MaterialListView material_listview;
    public static String TagID="TagID";
    public String orderId="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        super.setContentViewFor720_1280(R.layout.template_materiallistview_layout);
        findViewByIds();
        initDatas();
        TaskGetOrderDetail(false);
    }

    private void findViewByIds() {
        material_listview = (MaterialListView) findViewById(R.id.material_listview);
    }
    private void initDatas() {

        setTopTitle("订单详情");
        orderId=getIntent().getStringExtra(TagID);

    }
    private void fillArrayInListView( OrderDetailBean orderDetailBean) {
        OrderDetail1Card OrderDetail1Card=new OrderDetail1Card(this);
        OrderDetail1Card.setOrderDetailBean(orderDetailBean);
        material_listview.add(OrderDetail1Card);

        for (int i=0;i<orderDetailBean.getGoods().size();i++) {

                if (i==2 && orderDetailBean.getGoods().size()>3)
                {
                    OrderDetail2Card orderDetail1Card2 = new OrderDetail2Card(this);
                    orderDetail1Card2.setOrderDetailBean(orderDetailBean);
                    orderDetail1Card2.setMaterialListView(material_listview);
                    orderDetail1Card2.setGoodsEntity(orderDetailBean.getGoods().get(i));
                    orderDetail1Card2.setShowMore(true);
                    material_listview.add(orderDetail1Card2);
                }else if (i<3)
                {
                    OrderDetail2Card orderDetail1Card2 = new OrderDetail2Card(this);
                    orderDetail1Card2.setOrderDetailBean(orderDetailBean);
                    orderDetail1Card2.setMaterialListView(material_listview);
                    orderDetail1Card2.setGoodsEntity(orderDetailBean.getGoods().get(i));
                    material_listview.add(orderDetail1Card2);
                }

        }

        OrderDetail3Card OrderDetail1Card3=new OrderDetail3Card(this);
        OrderDetail1Card3.setOrderDetailBean(orderDetailBean);
        material_listview.add(OrderDetail1Card3);
    }

    private void TaskGetOrderDetail(final boolean isloadmore) {
        RequestServerManager.getInstanceDIY().handleMethodDIY((GBaseActivity) getActivity(), material_listview, isloadmore, null,
                Constant.getRootUrl() + Constant.Action.Action_OrderDetail, Constant.Action.Action_OrderDetail, RequestServerManager.getInstanceDIY().
                        getRequestParams(Constant.Action.Action_OrderDetail, App.getUserBean().getUserid(),App.getUserBean().getSession_id(),orderId), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        // JSONObject jsonObject = (JSONObject) result.getData();
                        OrderDetailBean orderDetailBean = JSON.parseObject(result.getData().toString(), OrderDetailBean.class);
                        fillArrayInListView(orderDetailBean);
                     //   App.saveOrderList(orderInfoBeanList);
                     //   App.getOrderList().clear();
                     //   App.getOrderList().addAll(orderInfoBeanList);
                    //    fillArrayInListView(App.getOrderList(),currentType);
                     //       Log.e("userBean",JSON.toJSONString(result.getData()));
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {

                    }
                });
    }
}